#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import time
import Image
import urllib
import MySQLdb
import StringIO
import datetime
import socket
import hashlib

socket.setdefaulttimeout(10)

def Score(data):
    pv = data[3]
    totalcomment = data[4]
    fav = data[5]
    updown = data[6] - data[7]
    reference = data[8]
    return fav

def GetFormat(t):
    if t < 10:
    	return "0" + str(t)
    return str(t)

def FormatTime(second):
    hour = GetFormat(second / 3600)
    min = GetFormat(second % 3600 / 60)
    sec = GetFormat(second % 60)
    return hour + ":" + min + ":" + sec

def MyCmp(data1, data2):
    if Score(data1) > Score(data2):
    	return -1
    return 1

def HisInter(h1, h2):
    up = 0
    down = 0
    down2 = 0
    for i in range(0, 768):
    	up = up + min(h1[i], h2[i])
    	down = down + h1[i]
    	down2 = down2 + h2[i]
    return min(1.0 * up / down, 1.0 * up / down2)

def Duplicate(thumb1, thumb2, duration1, duration2):
    if abs(duration2 - duration1) > 3:
    	return False
    return HisInter(thumb1.histogram(), thumb2.histogram()) > 0.8

def Deduplicate(l1, l2):
    thumbnails = []
    for i in range(0, video_cnt):
        try:
            data = urllib.urlopen(l1[i][2]).read()
            thumbnails.append(Image.open(StringIO.StringIO(data)))
        except:
            thumbnails.append('nothing')
            print 'image download error'
            l2.append(l1[i])
            continue
        label = True
        for j in range(0, i):
            try:
                if Duplicate(thumbnails[j], thumbnails[i], l1[j][11], l1[i][11]):
                    label = False
                    print j, i
                    break
            except:
                print 'image process error'
        if label:
            l2.append(l1[i])
    print len(thumbnails)

def DeduplicateMD5(l1, l2):
    md5s = {}
    for i in range(0, video_cnt):
        success = 0
        while success < 5:
            try:
                md5 = hashlib.md5(urllib.urlopen(l1[i][2]).read()).hexdigest()
                if not md5s.has_key(md5):
                    l2.append(l1[i])
                    md5s[md5] = 1
                break
            except Exception, data:
                print Exception, "XXX", data
                success = success + 1

def Normalize(data):
    pubdate = data[9]
    fav = data[5]
    timediff = cur_time -  time.mktime(pubdate.timetuple())
    if timediff > 10 * 24 * 3600:
        data[5] = fav
    else:
        data[5] = fav * 10 * 24 * 3600 / timediff

#得到0-30天前的日期 (0,30)
ISOTIMEFORMAT = '%Y-%m-%d'
ISOTIMEFORMAT2 = '%Y-%m-%d %H:%M:%S'
cur_time = time.time()
from_time = cur_time - 30 * 24 * 3600;
from_time_str = time.strftime(ISOTIMEFORMAT, time.localtime(from_time))
to_time = cur_time;
to_time_str = time.strftime(ISOTIMEFORMAT, time.localtime(to_time))

#连接数据库
conn = MySQLdb.Connection("172.16.7.58", "root", "researchsucks", "X", 3306)
conn.set_character_set("utf8");
cur = conn.cursor()

#执行查询
cur.execute("select id, title, imagelink,  pv, totalcomment, fav, up, down, reference, pubdate, playlink, duration, cats \
        from video_info where pubdate>=date('" + from_time_str + "') and pubdate<date('" + to_time_str + "')")

#基于流行度排序0-30天前视频
vlist = []
for data in cur.fetchall():
    data = list(data)
    data[5] = data[5] + min(100, data[8] / 50.0) + min(100, (data[6] - data[7]) / 50.0)
    Normalize(data)
    if data[5] > 200:
        vlist.append(data)

video_cnt = len(vlist)
vlist.sort(MyCmp)

#将top的视频信息写入xml, json
output = open('/home/chaojiansong/videobook/pop/ver1/pop_sorted.xml', 'w')
output2 = open('/home/chaojiansong/videobook/pop/ver1/pop_sorted.json', 'w')
output.write("<xml>\n")
output2.write("{\"images\":[\n")
output3 = open('/home/chaojiansong/videobook/pop/ver1/thumbnail_urls', 'w')

for i in range(0, video_cnt):
    output.write("<item>\n")
    output.write("<id>" + vlist[i][0] + "</id>\n")
    output.write("<title><![CDATA[" + vlist[i][1] + "]]></title>\n") 
    output.write("<thumbnail><![CDATA[" + vlist[i][2] + "]]></thumbnail>\n")
    output.write("<pv>" + str(vlist[i][3]) + "</pv>\n")
    output.write("<totalcomment>" + str(vlist[i][4]) + "</totalcomment>\n")
    output.write("<fav>" + str(vlist[i][5]) + "</fav>\n")
    output.write("<up>" + str(vlist[i][6]) + "</up>\n")
    output.write("<down>" + str(vlist[i][7]) + "</down>\n")
    output.write("<reference>" + str(vlist[i][8]) + "</reference>\n")
    output.write("<pubdate>" + str(vlist[i][9]) + "</pubdate>\n")
    output.write("<playlink><![CDATA[" + vlist[i][10] + "]]></playlink>\n")
    output.write("<duration>" + str(vlist[i][11]) + "</duration>\n")
    output.write("<cats>" + vlist[i][12] + "</cats>\n")
    output.write("</item>\n")
vlist2 = []
Deduplicate(vlist, vlist2)
#vlist2 = vlist
#DeduplicateMD5(vlist, vlist2)

tot = len(vlist2)
for i in range(0, tot):
    output2.write("{\"src\":\"" + vlist2[i][2] + "\", \"title\":\"" + re.sub(r'"', '\\"', vlist2[i][1]) + "\", \"href\":\"" + \
            vlist2[i][0] + "\", " + "\"playlink\":\"" + vlist2[i][10] + "\", \"duration\":\"" + FormatTime(vlist2[i][11]) +  "\"}")
    if i != tot - 1:
    	output2.write(',')
    output2.write('\n')
    #将视频记入pop_history
    cur.execute("insert ignore into pop_history(video_id) values('" + vlist2[i][0] + "')")

#记录视频缩略图
for i in range(0, video_cnt):
    output3.write(vlist[i][2] + '\n')

#将user_system_rec_pos归位;
cur.execute("update user_system_rec_pos set currentpos=1")
cur.execute("update user_system_rec_pos set nextpos=1")

output.write("</xml>\n")
output2.write("]}\n")
output.close()
output2.close()
output3.close()

cur.close()
conn.close()

#video_score_list
output4 = open('/home/chaojiansong/videobook/notinsvn/video_score_list', 'w')
for i in range(0, tot):
	output4.write(vlist2[i][0] + '\n')
	output4.write(str(vlist2[i][5]) + '\n')
	output4.write(vlist2[i][10] + '\n')
	output4.write(vlist2[i][2] + '\n')
	output4.write(vlist2[i][1] + '\n')
	output4.write(FormatTime(vlist2[i][11]) + '\n')
output4.close()
